<?php

namespace App\Models\Factory;

use App\Constants\CreditLogConstant;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\UserCredit;
use App\Models\Orm\UserCreditLog;
use App\Strategies\CreditStrategy;


/**
 * 完成个人资料获取积分工厂
 * Class SmsFactory
 * @package App\Models\Factory
 */
class IdentityCreditFactory extends AbsModelFactory
{

    //更新积分日志表

    public static function addIdentityCreditLog($userId,$income)
    {

        //积分余额
        $credit = UserCredit::where('user_id',$userId)->first();

        if($credit)
        {
            $credit= $credit->credits;
        }

        $credit = 0;

        //生成积分号
        $nid = CreditStrategy::creditNid();
        $creditLogObj['nid'] = $nid;
        $creditLogObj ['user_id']  = $userId;
        $creditLogObj['income']  = $income;
        $creditLogObj['surplus']    = $credit;
        $creditLogObj ['remark'] = CreditLogConstant::USERINFO_COMPLETE_REMARK;
        $creditLogObj['source'] = CreditLogConstant::USERINFO_COMPLETE_TYPE;
        $creditLogObj ['create_at']  = date('Y-m-d H:i:s', time());
        $creditLogObj ['create_ip'] = Utils::ipAddress();

        //首先判断表里是否有数据

       $result =  UserCreditLog::where(['user_id'=>$userId,'source'=> $creditLogObj['source']])->first();

        if($result)
        {
            return  false;
        }

        $re = UserCreditLog::create($creditLogObj);

        return $re;

    }




}